<?php

namespace App\Http\Controllers\Admin;

use App\Models\BingChuang;
use App\Models\Bingqu;
use App\Models\BingRen;
use App\Models\Remark;
use App\Models\SheBei;
use App\Models\TiWen;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class BingrenController extends Controller
{
    protected $fields = [
        'name' => '',
        'code' => '',
        'address' => '',
        'bingqu_id' => 0,
        'bingchuang_id' => 0,
    ];

    public function zhuyuan(Request $request)
    {
        $type = 1;
        $search = [
            'name'=>trim($request->input('name')),
            'bingqu_id'=>$request->input('bingqu_id'),
        ];
        $data = $this->get_data($request,$type);
        $bingqu = Bingqu::orderby('name','asc')->get();
        return view('admin.bingren.index',['data'=>$data,'search'=>$search,'type'=>$type,'bingqu'=>$bingqu]);
    }

    public function jujia(Request $request)
    {
        $type = 2;
        $search = [
            'name'=>trim($request->input('name')),
            'bingqu_id'=>$request->input('bingqu_id'),
        ];
        $data = $this->get_data($request,$type);
        $bingqu = Bingqu::orderby('name','asc')->get();
        return view('admin.bingren.index',['data'=>$data,'search'=>$search,'type'=>$type,'bingqu'=>$bingqu]);
    }

    public function get_data(Request $request,$type)
    {
        $sql = BingRen::where('status',$type);
        $request->input('name') && $sql->where('name','like','%'.trim($request->input('name')).'%');
        $request->input('bingqu_id') && $sql->where('bingqu_id',$request->input('bingqu_id'));
        $data = $sql->orderby('id','desc')->orderby('status','desc')->paginate(15);
        foreach ($data as $k => $v){
            $v->shebei_code = SheBei::where('bingren_id',$v->id)->value('code');
            $tiwen = TiWen::where('bingren_id',$v->id)->orderby('created_at','desc')->first();
            if($tiwen){
                $v->tiwen = $tiwen->tiwen;
                $v->xueyang = $tiwen->xueyang;
            }else{
                $v->tiwen = '';
                $v->xueyang = '';
            }
            $tiwen_max = TiWen::where('bingren_id',$v->id)->max('tiwen');
            $xueyang_min = TiWen::where('bingren_id',$v->id)->where('xueyang','>',0)->min('xueyang');
            if($tiwen_max >= 37.3 || (!empty($xueyang_min) && $xueyang_min < 95)){
                $v->is_hot = 1;
            }else{
                $v->is_hot = 0;
            }
        }
        return $data;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $data = [];
        foreach ($this->fields as $field => $default) {
            $data[$field] = old($field, $default);
        }
        $data['type'] = $request->input('type',1);
        $data['shebei'] = 0;
        $bingqu = Bingqu::orderBy('id','desc')->get();
        $bingchuang  = '';
        $shebei = SheBei::where('bingren_id',0)->take(5)->get();

        return view('admin.bingren.create',['data'=>$data,'bingqu'=>$bingqu,'bingchuang'=>$bingchuang,'shebei'=>$shebei,]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //dd($request->all());
        $bingren = new BingRen();
        foreach (array_keys($this->fields) as $field) {
            $bingren->$field = $request->get($field);
        }
        $bingren->status = $request->input('type',1);
        $bingren->save();

        SheBei::where('id',$request->input('shebei'))->update(['bingren_id'=>$bingren->id]);

        $url = $request->type ==1 ? '/admin/zhuyuan/index' : '/admin/jujia/index';

        return redirect($url)->withSuccess('添加成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $bingren = BingRen::find($id);
        if(!$bingren){
            //Toastr::error('找不到该数据！');
            return redirect('/admin/bingren')->withErrors('找不到该数据');
        }
        foreach (array_keys($this->fields) as $field) {
            $data[$field] = old($field, $bingren->$field);
        }
        $data['id'] = (int)$id;
        $bingqu = Bingqu::orderBy('id','desc')->get();
        $bingchuang  = BingChuang::where('bingqu_id',$bingren->bingqu_id)->get();
        $shebei = SheBei::where('bingren_id',$id)->get();
        $data['shebei'] = $shebei[0]->id;
        $data['status'] = $bingren->status;

        return view('admin.bingren.edit',['data'=>$data,'bingqu'=>$bingqu,'bingchuang'=>$bingchuang,'shebei'=>$shebei,]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //dd($request->all());
        $bingqu = BingRen::find((int)$id);

        foreach (array_keys($this->fields) as $field) {
            $bingqu->$field = trim($request->get($field));
        }
        $bingqu->save();

        $url = $bingqu->status ==1 ? '/admin/zhuyuan/index' : '/admin/jujia/index';

        return redirect($url)->withSuccess('编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function get_bingchuang(Request $request)
    {
        $return = [];
        do{
            $bingqu_id = $request->input('bingqu_id',1);
            $bingqu = Bingqu::where('id',$bingqu_id)->first();
            if(!$bingqu){
                $return['code'] = 101;
                $return['message'] = '病区不存在';
                break;
            }
            $bingchuang_used = BingRen::pluck('bingchuang_id')->toArray();
            $bingchuang = BingChuang::whereNotIn('id',$bingchuang_used)
                ->where('bingqu_id',$bingqu_id)->get();
            $return['code'] = 0;
            $return['message'] = '请求成功';
            $return['data'] = $bingchuang;
            break;
        }while(false);

        return response()->json($return);
    }

    public function tiwen($id)
    {
        $tiwen = TiWen::where('bingren_id',$id)->where('tiwen','<>',0)->orderby('created_at','asc')->get();
        $xueyang = TiWen::where('bingren_id',$id)->where('xueyang','<>',0)->orderby('created_at','asc')->get();
        $data['date_tiwen'] = [];
        $data['date_xueyang'] = [];
        $data['tiwen'] = [];
        $data['xueyang'] = [];
        foreach ($tiwen as $k => $v) {
            $data['date_tiwen'][] = date('m-d H:i',strtotime($v->created_at));
            $data['tiwen'][] = $v->tiwen;
        }
        foreach ($xueyang as $k => $v) {
            $data['date_xueyang'][] = date('m-d H:i',strtotime($v->created_at));
            $data['xueyang'][] = $v->xueyang;
        }
        $remark = Remark::where('bingren_id',$id)->orderby('created_at','desc')->get();

        return view('admin.bingren.info',['data'=>$data,'remark'=>$remark]);
    }

    public function jiechu($id)
    {
        $info = BingRen::where('id',$id)->first();
        return view('admin.bingren.jiechu',['info'=>$info]);
    }

    public function jiechu_post(Request $request)
    {
        //dd($request->all());
        $id = $request->input('id',0);
        $info = BingRen::where('id',$id)->first();
        if(!$info){
            return redirect()->back()->withErrors('不存在');
        }
        $status = $info->status;
        if($request->input('jiechu') == '治愈'){
            $info->status = 2;
        }

        $info->jiechu = trim($request->input('jiechu'));
        $info->save();

        //SheBei::where('bingren_id',$id)->update(['bingren_id'=>0]);

        $url = $status ==1 ? '/admin/zhuyuan/index' : '/admin/jujia/index';

        return redirect($url)->withSuccess('解除成功');

    }


}
